﻿<%@ WebHandler Language="C#" Class="Dic" %>

using System;
using System.Web;
using System.Web.SessionState;
public class Dic : IHttpHandler,IRequiresSessionState
{
    Spring.Context.IApplicationContext appContext = Spring.Context.Support.ContextRegistry.GetContext();
    GONES.Tools.Common.LoginInfo loginInfo = new GONES.Tools.Common.LoginInfo();
    public string ErrMsg { get; set; }
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string title = context.Request["title"];
        if (context.Session["LoginInfo"] != null)
        {
            loginInfo = (GONES.Tools.Common.LoginInfo)context.Session["LoginInfo"];
        }
        if (string.IsNullOrEmpty(title))
        {
            failure("无指定操作");
        }
        else
        {
            switch (title)
            {
                case "add":
                    if (Add(context.Request["dictitle"],context.Request["code"],context.Request["parentid"],
                       context.Request["status"],context.Request["sortnum"], context.Request["remark"]))
                    {
                        context.Response.Write(success("", "DicList"));
                    }
                    else
                    {
                        context.Response.Write(failure("数据库连接错误"));
                    }
                    break;
                case "delete":
                    if (Del(context.Request["ids"]))
                    {
                        context.Response.Write(success("", "DicList"));
                    }
                    else
                    {
                        context.Response.Write(failure(ErrMsg));
                    }
                    break;
                case "updata":
                    if (Updata(context.Request["dicid"],context.Request["dictitle"],context.Request["code"],
                    context.Request["parentid"],context.Request["status"],context.Request["sortnum"], context.Request["remark"]))
                    {
                        context.Response.Write(success("", "DicList"));
                    }
                    else
                    {
                        context.Response.Write(failure("修改失败!"));
                    }
                    break;
            }
        }
    }

    public bool Del(string id)
    {
        var dicManager = appContext.GetObject("S_DicManager") as GONES.IBLL.IS_DicManager;
        try
        {
            if (dicManager.GetDicCountByParentId(id) > 0)
            {
                ErrMsg = "该编码下有子编码不能删除！";
                return false;
            }
            dicManager.Delete(id);
            return true;
        }
        catch { return false; }
    }

    
    private GONES.Model.S_Dic GetDicModel(string id, string dictitle,string code, string parentid,string status, string sortnum, string remark)
    {
        var dicManager = appContext.GetObject("S_DicManager") as GONES.IBLL.IS_DicManager;
        GONES.Model.S_Dic dic = new GONES.Model.S_Dic();
        status = status == "on" ? "1" : "0";
        if (id.Trim() != "")
            dic = dicManager.Get(id.Trim());
        dic.ID = id.Trim();
        dic.Title = dictitle.Trim();
        dic.Code=code.Trim();
        dic.Parentid = parentid.Trim();
        dic.Status = int.Parse(status.Trim());
        dic.SortNum =int.Parse(sortnum.Trim());
        dic.Remark = remark.Trim();
       
        return dic;
    }
    public bool Add(string dictitle,string code, string parentid,string status, string sortnum, string remark)
    {
       var dicManager = appContext.GetObject("S_DicManager") as GONES.IBLL.IS_DicManager;
       GONES.Model.S_Dic dic = new GONES.Model.S_Dic();
        try
        {
            string id = "";
            dic = GetDicModel(id, dictitle,code, parentid, status,sortnum, remark);
            dic.ID = GONES.Tools.Common.Rnd.Guid.ToString().Trim();
            dic.CreateUser = loginInfo.UserName.Trim();
            dicManager.Add(dic);
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool Updata(string id,string dictitle,string code, string parentid,string status, string sortnum, string remark)
    {
       var dicManager = appContext.GetObject("S_DicManager") as GONES.IBLL.IS_DicManager;
       GONES.Model.S_Dic dic = new GONES.Model.S_Dic();
        try
        {
            dic = GetDicModel(id, dictitle,code, parentid, status,sortnum, remark);
            dic.ModifyUser = loginInfo.UserName;
            dicManager.Edit(dic);
            return true;
        }
        catch
        {
            return false;
        }
    }

    public string success()
    {
        return "{\"statusCode\":\"200\", \"message\":\"操作成功!\"}";
    }
    public string success(string str, string navTabId)
    {
        return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"" + navTabId + "\"}";
    }
    public string failure()
    {
        return "{\"statusCode\":\"300\", \"message\":\"操作失败\"}";
    }
    public string failure(string str)
    {
        return "{\"statusCode\":\"300\", \"message\":\"操作失败!" + str + "\"}";
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}